﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Caching;


namespace CentaLine.Infrastructure
{
        /// <summary>
        /// 缓存帮助类
        /// </summary>
        public static class CacheHelper
        {
            /// <summary>
            /// 默认绝对日期刷新时间（每天早上8.30过期）
            /// </summary>
            private static ExtendedFormatTime defExtendedFormatTime = new ExtendedFormatTime("30 8 * * *");

            /// <summary>
            /// 配置节点名称
            /// </summary>
            private static string cacheManagerName;

            /// <summary>
            /// 配置节点名称
            /// </summary>
            public static string CacheManagerName
            {
                get { return cacheManagerName; }
                set { cacheManagerName = value; }
            }

            /// <summary>
            /// 创建缓存管理类
            /// </summary>
            /// <returns>缓存管理类</returns>
            private static ICacheManager GetCacheManager()
            {
                return CacheFactory.GetCacheManager();
            }

            /// <summary>
            /// 获取指定键值的对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <returns>指定键值的对象</returns>
            public static object Get(string key)
            {
                return GetCacheManager()[key];
            }

            /// <summary>
            /// 添加缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            public static void Add(string key, object value)
            {
                GetCacheManager().Add(key, value);
            }

            /// <summary>
            /// 添加缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="minutes">缓存几分钟</param>
            public static void AddMinutes(string key, object value, int minutes)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(minutes)));
            }

            /// <summary>
            /// 添加缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="hours">缓存几小时</param>
            public static void AddHours(string key, object value, int hours)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromHours(hours)));
            }

            /// <summary>
            /// 添加缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="days">缓存几天</param>
            public static void AddDays(string key, object value, int days)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromDays(days)));
            }

            /// <summary>
            /// 添加默认绝对日期过期缓存对象（每天早上8.30过期）
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            public static void AddDefExtendedFormatTimeCache(string key, object value)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, defExtendedFormatTime);
            }

            /// <summary>
            /// 添加自定义绝对日期过期缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="timeFormat">每分钟过期（*****：5个*分别代表minute、hour、date、month、year）；每个小时的第5分钟过期（5****）；每个月的2号零点零分过期（0 0 2 * *）</param>
            public static void AddExtendedFormatTimeCache(string key, object value, string timeFormat)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, new ExtendedFormatTime(timeFormat));
            }

            /// <summary>
            /// 添加文件变动过期缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="FilePath">文件路径</param>
            public static void AddFileDependencyCache(string key, object value, string FilePath)
            {
                GetCacheManager().Add(key, value, CacheItemPriority.Normal, null, new FileDependency(FilePath));
            }

            /// <summary>
            /// 添加完全自定义过期缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            /// <param name="value">对象</param>
            /// <param name="scavengingPriority">重要级别</param>
            /// <param name="refreshAction">过期回调函数</param>
            /// <param name="expirations">过期规则</param>
            public static void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
            {
                GetCacheManager().Add(key, value, scavengingPriority, refreshAction, expirations);
            }

            /// <summary>
            /// 判断键值是否存在
            /// </summary>
            /// <param name="key">键值</param>
            /// <returns>键值是否存在</returns>
            public static bool Contains(string key)
            {
                return GetCacheManager().Contains(key);
            }

            /// <summary>
            /// 获取总共缓存数量
            /// </summary>
            /// <returns>数量</returns>
            public static Int32 Count()
            {
                return GetCacheManager().Count;
            }

            /// <summary>
            /// 移除指定键值缓存对象
            /// </summary>
            /// <param name="key">键值</param>
            public static void Remove(string key)
            {
                GetCacheManager().Remove(key);
            }

            /// <summary>
            /// 清空所有缓存对象
            /// </summary>
            public static void Flush()
            {
                GetCacheManager().Flush();
            }
        }
    
}
